package com.seo.utils;

import java.util.Random;

/**
 * SerialCodeUtil
 * @author Administrator 说明：日期处理 创建人：FH Q313596790
 * @version 2017.12.01 11:40
 */
public class SerialCodeUtil {

	private static final char[] R = new char[] {'q', 'w', 'e', '8', 'a', 's', '2', 'd', 'z', 'x', '9', 'c', '7', 'p',
			'5', 'i', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 'l', 't', 'n', '6', 'b', 'g', 'h' };

	/** (不能与自定义进制有重复) */
	private static final char B = 'o';

	/** 进制长度 */
	private static final int BE = R.length;

	/** 序列最小长度 */
	private static final int S = 6;

	/**
	 * 根据ID生成六位随机码
	 * 
	 * @param id
	 *            ID
	 * @return 随机码
	 */
	public static String toSerialCode(long id) {
		char[] buf = new char[32];
		int charPos = 32;

		while ((id / BE) > 0) {
			int ind = (int) (id % BE);
			// System.out.println(num + "-->" + ind);
			buf[--charPos] = R[ind];
			id /= BE;
		}
		buf[--charPos] = R[(int) (id % BE)];
		String str = new String(buf, charPos, (32 - charPos));
		// 不够长度的自动随机补全
		if (str.length() < S) {
			StringBuilder sb = new StringBuilder();
			sb.append(B);
			Random rnd = new Random();
			for (int i = 1; i < S - str.length(); i++) {
				sb.append(R[rnd.nextInt(BE)]);
			}
			str += sb.toString();
		}
		return str;
	}

	public static void main(String [] args){
		System.out.println(toSerialCode(4));
		System.out.println(toSerialCode(5));
		System.out.println(toSerialCode(7));
		System.out.println(toSerialCode(8));
	}
}
